<?php

namespace QyManage\Controller;

use QyManage\Controller\QyManageController;

/**
 * 企业管理-企业设置
 */
class SettingController extends QyManageController {

    /**
     *  企业信息
     */
    public function detail() {
        $strCompany_id = $this->iCompanyId;

        $where['qy_company.company_id'] = $strCompany_id;
        $data = D('Company')
                ->field('qy_company.company_id,
                     qy_company.company_name,
                     qy_company.description,
                     qy_company.base_num,
                     qy_company.create_time,
                     qy_company.sever_end_time,
                	 qy_company.status
                ')
                ->where($where)
                ->find();
        $relatCount = D('UserCompanyRelat')->where("status >= 0 and company_id = $strCompany_id")->count('1');

        $info['company_id'] = $data['company_id'];
        $info['company_name'] = $data['company_name'];
        $info['description'] = $data['description'];
        $info['menber_num'] = $relatCount;
        $info['menber_total'] = $data['base_num'];
        $info['create_time'] = date('Y年m月d日', strtotime($data['create_time']));
        $info['sever_end_time'] = date('Y年m月d日', strtotime($data['sever_end_time']));
        $info['status'] = $data['status'];

        $this->returnList($info);
    }

    /**
     *  企业信息编辑
     */
    public function edit() {
        $userId = $this->iUserId;
        $strCompany_id = $this->iCompanyId;
        $strDescription = I('description', '', 'strip_tags,htmlspecialchars');

        $Company = D("Company");
        $id = $Company->where(array('company_id' => $strCompany_id))->getField('user_id');
        if ($userId != $id) {
            $this->error('无权限操作！');
        }
        $Company = D("Company");
        $Company->company_id = $strCompany_id;
        $Company->description = $strDescription;
        $Company->update_time = date('Y-m-d H:i:s', time());
        $res = $Company->save();

        $this->success('编辑成功！');
    }

    /**
     *  退出企业
     */
    public function secede() {

        $role_id = $this->iRoleId;
        $company_id = $this->iCompanyId;
        $userId = $this->iUserId;
        $Relat = D('UserCompanyRelat');

        if ($role_id == 1) {
            $this->error('拥有者不能退出企业');
        }
        $relatId = $Relat->where(array("company_id" => $company_id, "user_id" => $userId))->getField('id');

        $Relat->id = $relatId;
        $Relat->status = -1;
        $res = $Relat->save();

        if ($res) {
            set_redis_userinfo($userId, 'iCompanyId', 0);
            set_redis_userinfo($userId, 'iPostId', 0);
            set_redis_userinfo($userId, 'iRoleId', 0);
            set_redis_userinfo($userId, '');
//            session('iCompanyId', null);   //企业ID
//            session('iPostId', null); //岗位ID
//            session('iRoleId', null); //角色ID
//            session('iStatus', -1); //状态ID
            $this->success('退出成功！');
        } else {
            $this->error('退出失败！');
        }
    }

}
